package com.cvte.boe.media.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;

public final class ParseEncoding extends Encode {

    private static ParseEncoding parseEncoding;

    private ParseEncoding() {
        gb2312format = (int[][]) Array.newInstance(Integer.TYPE, new int[]{94, 94});
        gbkformat = (int[][]) Array.newInstance(Integer.TYPE, new int[]{126, 191});
        big5format = (int[][]) Array.newInstance(Integer.TYPE, new int[]{94, 158});
        euckrformat = (int[][]) Array.newInstance(Integer.TYPE, new int[]{94, 94});
        jpformat = (int[][]) Array.newInstance(Integer.TYPE, new int[]{94, 94});
        init();
    }

    public static void initParseEncoding() {
        if (parseEncoding == null) {
            parseEncoding = new ParseEncoding();
        }
    }

    public static String getEncoding(String path) {
        return check(parseEncoding.getEncodeValue(path));
    }

    public static String getEncoding(InputStream in) {
        return check(parseEncoding.getEncodeValue(in));
    }

    public static String getEncoding(byte[] buffer) {
        return check(parseEncoding.getEncodeValue(buffer));
    }

    public static String getEncoding(URL url) {
        return check(parseEncoding.getEncodeValue(url));
    }

    private static String check(int result) {
        if (result == -0x1) {
            return javaname[0x9];
        }
        return javaname[result];
    }

    private int getEncodeValue(String path) {
        int express = 0x9;
        if (path.startsWith("http://")) {
            try {
                express = getEncodeValue(new URL(path));
            } catch (MalformedURLException e) {
                express = -0x1;
                return express;
            }
            return express;
        }
        return getEncodeValue(new File(path));
    }

    public int getEncodeValue(InputStream in) {
        byte[] arrayOfByte = new byte[8192];
        int i = 0;
        try {
            while (true) {
                int j = in.read(arrayOfByte, i, arrayOfByte.length - i);
                if (j <= 0)
                    break;
                i += j;
            }
            in.close();
            int k = getEncodeValue(arrayOfByte);
            return k;
        } catch (Exception localException) {
        }
        return -1;
    }

    public int getEncodeValue(URL url) {
        try {
            InputStream localInputStream = url.openStream();
            return getEncodeValue(localInputStream);
        } catch (IOException localIOException) {
        }
        return -1;
    }

    public int getEncodeValue(File file) {
        if (file == null || !file.exists() || !file.isFile()) return -1;
        try {
            FileInputStream inputStream = new FileInputStream(file);
            return getEncodeValue(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }

    private final byte[] read(InputStream inputStream) {
        byte[] arrayByte = new byte[]{};
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[0x2000];
        try {
            bytes = new byte[inputStream.available()];
            int read = -1;
            while ((read = inputStream.read(bytes)) >= 0) {
                byteArrayOutputStream.write(bytes, 0x0, read);
            }
            return arrayByte;
        } catch (IOException e) {
            return null;
        }
    }

    public int getEncodeValue(byte[] content) {
        if (content == null) {
            return -0x1;
        }
        int maxscore = 0x0;
        int encoding = 0x9;
        int[] scores = new int[0xa];
        scores[0x0] = gb2312probability(content);
        scores[0x1] = gbkprobability(content);
        scores[0x2] = big5probability(content);
        scores[0x3] = utf8probability(content);
        scores[0x4] = utf16probability(content);
        scores[0x5] = euckrprobability(content);
        scores[0x8] = asciiprobability(content);
        scores[0x6] = sjisprobability(content);
        scores[0x7] = eucjpprobability(content);
        scores[0x9] = 0x0;
        for (int index = 0x0; index < 0xa; index = index + 0x1) {
            if (scores[index] > maxscore) {
                encoding = index;
                maxscore = scores[index];
            }
        }
        if (maxscore <= 0x32) {
            return encoding;
        }
        return encoding;
    }

    private int gb2312probability(byte[] content) {
        int dbchars = 0x1;
        int gbchars = 0x1;
        long gbformat = 0x0;
        long totalformat = 0x1;
        float rangeval = 0x0;
        float formatval = 0x0;
        int rawtextlen = content.length;
        for (int i = 0x0; i < (rawtextlen - 0x1); i = i + 0x1) {
            if (content[i] < 0) {
                dbchars = dbchars + 0x1;
                if ((-0x5f <= content[i]) && (content[i] <= -0x9) && (-0x5f <= content[(i + 0x1)]) && (content[(i + 0x1)] <= -0x2)) {
                    gbchars = gbchars + 0x1;
                    totalformat += 0x1f4;
                    int row = (content[i] + 0x100) - 0xa1;
                    int column = (content[(i + 0x1)] + 0x100) - 0xa1;
                    if (gb2312format[row][column] != 0) {
                        gbformat += (long) gb2312format[row][column];
                    } else if ((0xf <= row) && (row < 0x37)) {
                        gbformat += 0xc8;
                    }
                }
                i = i + 0x1;
            }
        }
        rangeval = 50.0f * ((float) gbchars / (float) dbchars);
        formatval = 50.0f * ((float) gbformat / (float) totalformat);
        return (int) (rangeval + formatval);
    }

    private int gbkprobability(byte[] content) {
        //Fixme: gbk encoding
//        int i = 1;
//        int j = 1;
//        long l1 = 0L;
//        long l2 = 1L;
//        int k = content.length;
//        int m = 0;
//        if (m < k - 1)
//        {
//            int i2;
//            if (content[m] < 0)
//            {
//                i++;
//                if ((-95 > content[m]) || (content[m] > -9) || (-95 > content[(m + 1)]) || (content[(m + 1)] > -2))
//                    break label180;
//                j++;
//                l2 += 500L;
//                i2 = -161 + (256 + content[m]);
//                int i3 = -161 + (256 + content[(m + 1)]);
//                if (this.gb2312format[i2][i3] == 0)
//                    break label155;
//                l1 += this.gb2312format[i2][i3];
//            }
//            label155: label180: label348:
//            while (true)
//            {
//                m++;
//                m++;
//                break;
//                if ((15 > i2) || (i2 >= 55))
//                    continue;
//                l1 += 200L;
//                continue;
//                if ((-127 > content[m]) || (content[m] > -2) || (((-128 > content[(m + 1)]) || (content[(m + 1)] > -2)) && ((64 > content[(m + 1)]) || (content[(m + 1)] > 126))))
//                    continue;
//                j++;
//                l2 += 500L;
//                int n = -129 + (256 + content[m]);
//                if ((64 <= content[(m + 1)]) && (content[(m + 1)] <= 126));
//                for (int i1 = -64 + content[(m + 1)]; ; i1 = -64 + (256 + content[(m + 1)]))
//                {
//                    if (this.gbkformat[n][i1] == 0)
//                        break label348;
//                    l1 += this.gbkformat[n][i1];
//                    break;
//                }
//            }
//        }
//        return -1 + (int)(50.0F * (j / i) + 50.0F * ((float)l1 / (float)l2));
        return 0;
    }

    private int big5probability(byte[] content) {
        //Fixme: Big5 encoding
//        int i = 1;
//        int j = 1;
//        long l1 = 0L;
//        long l2 = 1L;
//        int k = content.length;
//        int m = 0;
//        if (m < k - 1) {
//            int n;
//            int i1;
//            if (content[m] < 0) {
//                i++;
//                if ((-95 <= content[m]) && (content[m] <= -7) && (((64 <= content[(m + 1)]) && (content[(m + 1)] <= 126)) || ((-95 <= content[(m + 1)]) && (content[(m + 1)] <= -2)))) {
//                    j++;
//                    l2 += 500L;
//                    n = -161 + (256 + content[m]);
//                    if ((64 > content[(m + 1)]) || (content[(m + 1)] > 126))
//                        break label194;
//                    i1 = -64 + content[(m + 1)];
//                    label156:
//                    if (this.big5format[n][i1] == 0)
//                        break label212;
//                    l1 += this.big5format[n][i1];
//                }
//            }
//            while (true) {
//                m++;
//                m++;
//                break;
//                label194:
//                i1 = -97 + (256 + content[(m + 1)]);
//                break label156;
//                label212:
//                if ((3 > n) || (n > 37))
//                    continue;
//                l1 += 200L;
//            }
//        }
//        return (int) (50.0F * (j / i) + 50.0F * ((float) l1 / (float) l2));
        return 0;
    }

    private int utf8probability(byte[] content) {
        int score = 0x0;
        int goodbytes = 0x0;
        int asciibytes = 0x0;
        int rawtextlen = content.length;
        for (int i = 0x0; i < rawtextlen; i = i + 0x1) {
            if ((content[i] & 0x7f) == content[i]) {
                asciibytes = asciibytes + 0x1;
            } else if ((-0x40 <= content[i]) && (content[i] <= -0x21) && ((i + 0x1) < rawtextlen) && (-0x80 <= content[(i + 0x1)]) && (content[(i + 0x1)] <= -0x41)) {
                goodbytes = goodbytes + 0x2;
                i = i + 0x1;
            } else if ((-0x20 <= content[i]) && (content[i] <= -0x11) && ((i + 0x2) < rawtextlen) && (-0x80 <= content[(i + 0x1)]) && (content[(i + 0x1)] <= -0x41) && (-0x80 <= content[(i + 0x2)]) && (content[(i + 0x2)] <= -0x41)) {
                goodbytes = goodbytes + 0x3;
                i = i + 0x2;
            }
        }
        if (asciibytes == rawtextlen) {
            return 0x0;
        }
        score = (int) (100.0f * ((float) goodbytes / (float) (rawtextlen - asciibytes)));
        if (score > 0x62) {
            return score;
        }
        if ((score > 0x5f) && (goodbytes > 0x1e)) {
            return score;
        }
        return 0x0;
    }

    private int utf16probability(byte[] content) {
        if ((content.length > 0x1) && (-0x2 == content[0x0]) && (-0x1 == content[0x1])
                || (-0x1 == content[0x0]) && (-0x2 == content[0x1])) {
            return 0x64;
        }
        return 0x0;
    }

    private int asciiprobability(byte[] content) {
        int score = 0x4b;
        int rawtextlen = content.length;
        for (int i = 0x0; i < rawtextlen; i = i + 0x1) {
            if (content[i] < 0) {
                score = score - 0x5;
            } else if (content[i] == 0x1b) {
                score = score - 0x5;
            }
            if (score <= 0) {
                return 0x0;
            }
        }
        return score;
    }

    private int euckrprobability(byte[] content) {
        int dbchars = 0x1;
        int krchars = 0x1;
        long krformat = 0x0;
        long totalformat = 0x1;
        float rangeval = 0x0;
        float formatval = 0x0;
        int rawtextlen = content.length;
        for (int i = 0x0; i < (rawtextlen - 0x1); i = i + 0x1) {
            if (content[i] < 0) {
                dbchars = dbchars + 0x1;
                if ((-0x5f <= content[i]) && (content[i] <= -0x2) && (-0x5f <= content[(i + 0x1)]) && (content[(i + 0x1)] <= -0x2)) {
                    krchars = krchars + 0x1;
                    totalformat += 0x1f4;
                    int row = (content[i] + 0x100) - 0xa1;
                    int column = (content[(i + 0x1)] + 0x100) - 0xa1;
                    if (euckrformat[row][column] != 0) {
                        krformat += (long) euckrformat[row][column];
                    } else if ((0xf <= row) && (row < 0x37)) {
                        krformat += 0x0;
                    }
                }
                i = i + 0x1;
            }
        }
        rangeval = 50.0f * ((float) krchars / (float) dbchars);
        formatval = 50.0f * ((float) krformat / (float) totalformat);
        return (int) (rangeval + formatval);
    }

    private int eucjpprobability(byte[] content) {
        int dbchars = 0x1;
        int jpchars = 0x1;
        long jpFormat = 0x0;
        long totalformat = 0x1;
        float rangeval = 0x0;
        float formatval = 0x0;
        int rawtextlen = content.length;
        for (int i = 0x0; i < (rawtextlen - 0x1); i = i + 0x1) {
            if (content[i] < 0) {
                dbchars = dbchars + 0x1;
                if ((-0x5f <= content[i]) && (content[i] <= -0x2) && (-0x5f <= content[(i + 0x1)]) && (content[(i + 0x1)] <= -0x2)) {
                    jpchars = jpchars + 0x1;
                    totalformat += 0x1f4;
                    int row = (content[i] + 0x100) - 0xa1;
                    int column = (content[(i + 0x1)] + 0x100) - 0xa1;
                    if (jpformat[row][column] != 0) {
                        jpFormat += (long) jpformat[row][column];
                    } else if ((0xf <= row) && (row < 0x37)) {
                        jpFormat += 0x0;
                    }
                }
                i = i + 0x1;
            }
        }
        rangeval = 50.0f * ((float) jpchars / (float) dbchars);
        formatval = 50.0f * ((float) jpFormat / (float) totalformat);
        return (int) (rangeval + formatval);
    }

    private int sjisprobability(byte[] content) {
        //Fixme: sjis encoding
//        int i = 1;
//        int j = 1;
//        long l1 = 0L;
//        long l2 = 1L;
//        int k = content.length;
//        int m = 0;
//        if (m < k - 1) {
//            int n;
//            int i1;
//            int i2;
//            if (content[m] < 0) {
//                i++;
//                if ((m + 1 < content.length) && (((-127 <= content[m]) && (content[m] <= -97)) || ((-32 <= content[m]) && (content[m] <= -17) && (((64 <= content[(m + 1)]) && (content[(m + 1)] <= 126)) || ((-128 <= content[(m + 1)]) && (content[(m + 1)] <= -4)))))) {
//                    j++;
//                    l2 += 500L;
//                    n = 256 + content[m];
//                    i1 = 256 + content[(m + 1)];
//                    if (i1 >= 159)
//                        break label279;
//                    i2 = 1;
//                    if (i1 <= 127)
//                        break label270;
//                    (i1 - 32);
//                    label182:
//                    if (n >= 160)
//                        break label291;
//                }
//            }
//            label270:
//            label279:
//            label291:
//            for (int i3 = (n - 112 << 1) - i2; ; i3 = (n - 176 << 1) - i2) {
//                int i4 = i3 - 32;
//                if ((i4 < this.jpformat.length) && (32 < this.jpformat[i4].length) && (this.jpformat[i4][32] != 0))
//                    l1 += this.jpformat[i4][32];
//                m++;
//                m++;
//                break;
//                (i1 - 25);
//                break label182;
//                (i1 - 126);
//                i2 = 0;
//                break label182;
//            }
//        }
//        return -1 + (int) (50.0F * (j / i) + 50.0F * ((float) l1 / (float) l2));
        return 0;
    }
}
